package com.example.cinema.fragment;

import android.app.Application;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.cinema.BR;
import com.example.cinema.CountListHeight;
import com.example.cinema.R;
import com.example.cinema.UserAttribute;
import com.example.cinema.adapter.CommentAdapter;
import com.example.cinema.adapter.CommentRecyAdapter;
import com.example.cinema.bindingdata.Advert;
import com.example.cinema.bindingdata.CircleItemData;
import com.example.cinema.bindingdata.VideoData;
import com.example.cinema.database.AppDataBase;
import com.example.cinema.database.DataBaseMethod;
import com.example.cinema.databinding.VideoAppraiseBinding;
import com.example.cinema.request.RequestData;
import com.example.cinema.request.RequestSend;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//影视评论
public class VideoAppraise extends Fragment {

    private VideoAppraiseBinding VAP;



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        VAP = DataBindingUtil.inflate(inflater, R.layout.video_appraise,container,false);

        return VAP.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    //外部传值
    private static String vid;
    private static String tid;
    private int page = 1;
    private int limit = 50;
    public void setVideid(String vid,String tiv)
    {
        this.vid = vid;
        this.tid = tiv;
    }

    public int zanindex;
    Handler handler = new Handler(Looper.getMainLooper())
    {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if(msg.what == 1000)
            {

                if(ListData.size()<1)
                {
                    NullListener.IsNull(true);
                }else {
                    NullListener.IsNull(false);
                }

                //插入评价
                Log.i("HHHHS",ListData.size()+"数量");
                for (int i=0;i<ListData.size();i++)
                {
                    ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.circle_item,null,false);
                    dataBinding.setVariable(BR.circledata,ListData.get(i));
                    Log.i("JHHHD",ListData.get(0).userimage.get());
                    int finalI = i;
                    dataBinding.getRoot().findViewById(R.id.zan).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            zanindex = finalI;
                            CircleItemData itemData = ListData.get(finalI);
                            ImageView iv = (ImageView) v;
                            iv.getDrawable().setLevel(2);
                            SenZan(itemData);
                        }
                    });
                    VAP.listview.addView(dataBinding.getRoot());
                    Log.i("AAAA","执行插入操作");
                }
            }

            if(msg.what == 1002)
            {
                boolean b = (boolean) msg.obj;
                if(b)
                {
                    int z = Integer.valueOf(ListData.get(zanindex).zannumbes.get());
                    ListData.get(zanindex).zannumbes.set(String.valueOf(z+1));
                    Toast toast = Toast.makeText(getContext(),"点赞成功",Toast.LENGTH_LONG);
                    toast.setGravity(Gravity.CENTER,0,0);
                    toast.show();
                }

            }

            if(msg.what == 1003)
            {
                List<CircleItemData> cdata = (List<CircleItemData>) msg.obj;
                for (int s=0;s<cdata.size();s++)
                {

                    Log.i("WEEW",cdata.get(s).userimage.get());

                    ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.circle_item,null,false);
                    dataBinding.setVariable(BR.circledata,cdata.get(s));

                    int finalS = s;
                    dataBinding.getRoot().findViewById(R.id.zan).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ImageView iv = (ImageView) v;
                            iv.getDrawable().setLevel(2);
                            Log.i("RRRR",finalS+"");
                            String num =  cdata.get(finalS).zannumbes.get();
                            Log.i("RRRR",num+"");
                            int nnn = Integer.valueOf(num)+1;
                            cdata.get(finalS).zannumbes.set(String.valueOf(nnn));
                            RequestSend requestSend = new RequestSend(getContext());
                            new Thread(){
                                @Override
                                public void run() {
                                    boolean bol = requestSend.SendUserZan(vid,cdata.get(finalS).usersid.get());
                                }
                            }.start();

                        }
                    });
                    VAP.listview.addView(dataBinding.getRoot());
                }
            }
            if(msg.what == 1004)
            {
                boolean bb = (boolean) msg.obj;
                if(bb)
                {
                    Toast.makeText(getContext(),"点赞成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(getContext(),"点赞失败",Toast.LENGTH_SHORT).show();
                }
            }
        }
    };


    private List<CircleItemData> ListData = new ArrayList<>();//评价数据
    private void init() {

        DataBaseMethod  baseMethod = new DataBaseMethod(getContext(),"adverdata");
        List<Advert> adverts = baseMethod.GetPlayAdv();
        if(adverts.size()>0)
        {
            AdverItemFragment adveritem = new AdverItemFragment();
            adveritem.setData(adverts.get(0));
            FragmentManager manager1 = getChildFragmentManager();
            FragmentTransaction Ft1 = manager1.beginTransaction();
            Ft1.add(R.id.listview,adveritem,"aa");
            Ft1.commit();
        }
        GetAddsData();
    }
    //获取评价内容
    private void GetAddsData()
    {
        new Thread()
        {
            @Override
            public void run() {
                //获取评价内容
                RequestData requestData = new RequestData(getContext());
                List<CircleItemData> cdata = requestData.GetAppraise(vid,1,30);
                Message message = new Message();
                message.what = 1003;
                message.obj = cdata;
                handler.sendMessage(message);

            }
        }.start();


    }
    //评论点赞处理
    private void SenZan(CircleItemData data)
    {
        Log.i("YYY",data.msgcontext.get());
        Log.i("YYY",data.userimage.get());
        Log.i("YYY",data.zannumbes.get());
        Log.i("YYY",data.usersid.get());
        new Thread()
        {
            @Override
            public void run() {
                RequestSend send = new RequestSend(getContext());
                boolean boo = send.SendUserZan(vid,data.usersid.get());
                Message message = new Message();
                message.what = 1002;
                message.obj = boo;
                handler.sendMessage(message);
            }
        }.start();
    }

    //添加评论及时显示
    public void  SetAssess(String text,UserAttribute attribute)
    {
        SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String tim = SDF.format(new Date());
        CircleItemData data = new CircleItemData();
        data.zannumbes.set("0");
        data.usersid.set(attribute.getUID());
        data.usertimes.set(tim);
        data.username.set(attribute.getUname());
        data.msgcontext.set(text);
        data.userimage.set(attribute.getUimage());
        List<CircleItemData> linshi = new ArrayList<>();
        linshi.add(data);
        ViewDataBinding dataBinding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.circle_item,null,false);
        dataBinding.setVariable(BR.circledata,linshi.get(0));
        dataBinding.getRoot().findViewById(R.id.zan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageView iv = (ImageView) v;
                iv.getDrawable().setLevel(2);
                RequestSend requestSend = new RequestSend(getContext());
                new Thread()
                {
                    @Override
                    public void run() {
                        boolean bool = requestSend.SendUserZan(vid,attribute.getUID());
                        Message msg = new Message();
                        msg.what = 1004;
                        msg.obj = bool;
                        handler.sendMessage(msg);
                    }
                }.start();
            }
        });
        VAP.listview.addView(dataBinding.getRoot());
    }

    ///评价数量回调
    public interface  ListCountIsNullListener{
        void IsNull(boolean abool);
    }
    private ListCountIsNullListener NullListener;
    public void SetListCountIsNull(ListCountIsNullListener listCountIsNullListener)
    {
        NullListener = listCountIsNullListener;
    }

}
